/**
 * Created by lesonchan on 2017/9/30.
 */
import Vue from "vue";
import axios from "axios";
let signLink = location.href.split('#')[0]

// axios 配置
axios.defaults.timeout = 10000;
axios.defaults.baseURL = 'http://dgj.gzqnyf.com';


// http request 拦截器
axios.interceptors.request.use(
  config => {
    
    const OPENID = localStorage.openId
    if (OPENID) {
      let tokens = btoa(OPENID + ':')
      config.headers.Authorization = `Basic ${tokens}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });
  
// http response 拦截器
axios.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    if (error.response.status === 401) {
      switch (error.response.request.responseText) {
        case '0':
          break
        case '1':
          break
        case '2':
          break
        case '3':
          if(signLink.indexOf('work.html')>0 || signLink.indexOf('info.html')>0) {
            window.location = 'index.html#/reg?type=0&path=reginfo'
          }else {
            if(signLink.indexOf('index.html')>0) {
              window.location = 'index.html#/reg?type=1&path=trainlist'
            }else {
              window.location = 'index.html#/reg?type=1&path=train'
            }
          }
          break
        case '4':
            window.location = 'index.html#/reginfo'
          break
        case '5':
            window.location = 'index.html#/regtips'
          break
      }
    } else {
      //alert('WEB:' + error.response.data.message)
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response.data)
  });

export default axios;
